const redis = require('redis')
const client = redis.createClient(6379, '127.0.0.1', {})

const query = function(info,callback){
    var type = {all:Math.round(Math.random()),male:0,female:1};
    info.type = info.type || 'all';
    client.SELECT(type[info.type],function(){
        /*随机返回当前数据库的一个键*/
        client.RANDOMKEY(function(err,bottleId){
            if(!bottleId){
                return callback({code:0,msg:"大海空空如也..."});
            }
            /*根据key返回哈希对象*/
            client.HGETALL(bottleId,function(err,bottle){
                if(err){
                    return callback({code:0,msg:"漂流瓶破损了..."});
                }
                callback({code:1,msg:bottle});
                /*根据key删除键值*/
                client.DEL(bottleId);
            });
        });
    });
}

module.exports = query
